
#include "CGUIObjManager.h"
#include "CGUIControl.h"
#include "CGUIWinControl.h"
#include "CGUIButton.h"
#include "CGUIFileLoader.h"
#include "CGUIGui.h"
CGUIObjManager::CGUIObjManager(void)
{

	m_pCurCtrl=NULL;
}

CGUIObjManager::~CGUIObjManager(void)
{
}


CGUIObjManager& CGUIObjManager::Instance(void)
{
	static CGUIObjManager instance;
	return instance;
}
void CGUIObjManager::CreateForm(const TiXmlElement* element)
{
	WCHAR szPosition[MAX_PATH];
	WCHAR szName[MAX_PATH];
	WCHAR szBmp[MAX_PATH];
	
	CharToWide(szName,element->Attribute("name"));
	CharToWide(szPosition,element->Attribute("position"));	
	CharToWide(szBmp,element->Attribute("bitmap"));

	//CGUIWinControl pCtrl;
	//pCtrl.Create(NULL, 0, 0, 480, 272, RGB16(200, 200, 0));
//m_pmapCtrls->insert(str_vec_pair(szName,m_vecpCtrls));
//m_vecpCtrls.clear();
//m_vecpCtrls.push_back(pCtrl);
//m_pCurCtrl=&pCtrl;

}

void CGUIObjManager::CreateButton(const TiXmlElement* element)
{



	WCHAR szPosition[MAX_PATH]={0};
	WCHAR szName[MAX_PATH]={0};
	WCHAR szTarget[MAX_PATH]={0};
	WCHAR szBmp[MAX_PATH]={0};

	CharToWide(szName,element->Attribute("name"));
	CharToWide(szPosition,element->Attribute("position"));	
	CharToWide(szBmp,element->Attribute("bitmap"));
	CharToWide(szTarget,element->Attribute("target"));
	CGUIWinControl *pCtrl1=new CGUIWinControl;
	pCtrl1->Create(NULL, 0, 0, 480, 272, RGB16(200, 200, 0));
	CGUIButton *pCtrl=new CGUIButton;
   

	CGUIFileLoader Loader;

    Loader.SetDirApp(_T("Pic"));
	m_btnBmp.LoadBMP(Loader.Load(_T("btn.bmp")));

    pCtrl->Create(pCtrl1, 49, 128, m_btnBmp, NULL);

	pCtrl->SetDownOffset(-1, 3);
	m_vecpCtrls.push_back(pCtrl);
	//m_pCurCtrl=pCtrl;

    


}
void CGUIObjManager::Parse(TiXmlNode *this_node,  TCHAR *filename)
{
	TiXmlElement* element = NULL;
	TiXmlNode *node = NULL;
	const char *value = NULL;
	TCHAR  szFilePath[MAX_PATH]={0};

	TiXmlDocument doc;
	bool bExternalLoad = false;
 
	
 
	if(filename != L"")
	{
		wcscpy(szFilePath, GetAppPath());
		wcscat(szFilePath,filename);
		FILE * hF = _wfopen(szFilePath,L"rb");
		if(!doc.LoadFile(hF))	// Try to parse data using given filename
		{

			return ;
		}
       fclose(hF);
		node = doc.FirstChild();	// Aquire first data node
		bExternalLoad = true;
	}

	if(this_node !=NULL)
	{
		element = this_node->ToElement();

		if(element->Attribute("File") !=NULL)
		{
			if(!doc.LoadFile(element->Attribute("File")))
			{

				return ;
			}

			node = doc.FirstChild();
			bExternalLoad = true;
		}
	}

	if(bExternalLoad)
	{
		element = node->ToElement();

		
		if(value!= NULL)
		{
	
		}

	

		Parse(node, L"");

	}
	else
	{
		
		element = this_node->ToElement();

		value = element->Attribute("position");
	

		TiXmlNode *child_node = NULL;
		child_node = this_node->FirstChild();

		while(child_node !=NULL)
		{
			//CGUIControl *newElement = NULL;
			if(stricmp(child_node->Value(), "Frame") == 0)	// We got a Frame element
				//newElement = new CGUIWinControl;
				CreateForm(child_node->ToElement());
			else if (stricmp(child_node->Value(), "Button") == 0)
				CreateButton(child_node->ToElement());

		

				//newElement = new CGUIButton;
			//if(stricmp(child_node->Value(), "static") == 0)	// We got a static control
			//	newElement = new CGUIStatic;
            
		
			Parse(child_node, L"");
			//if(Parse(child_node, L""))
			//{
			//	// Each desktop will have a full screen for its needs
			//
			//}
			//else
			//	delete newElement;

			child_node = this_node->IterateChildren(child_node);
		}
	}

}